package com.yuejie.musicai.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.yuejie.musicai.beans.ScoreInfo;
import com.yuejie.musicai.db.DBConnection;
import com.yuejie.musicai.log.LogLevelEnum;
import com.yuejie.musicai.log.LogServiceImpl;
import com.yuejie.musicai.services.ScoreService;

@Service
public class ScoreServiceImpl implements ScoreService {

	@Override
	public ScoreInfo query(String user, String videoid) {
		DBConnection db = new DBConnection();
		int score = 0;
		int unlike = 0;
		Object[] params = new Object[]{videoid};
		ScoreInfo scoreInfo = new ScoreInfo();
		String mess = "success";
		boolean op = true;
		try
		{
			List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, params);
			
			if(list != null && list.size() != 0) 
			{
				Map<String, Object> map = list.get(0);
				if(map.get("music_score") != null && !"".equals(map.get("music_score").toString())) {
					score = Integer.parseInt(map.get("music_score").toString());
				}
				
				if(map.get("music_unlike") != null && !"".equals(map.get("music_unlike").toString())) {
					unlike = Integer.parseInt(map.get("music_unlike").toString());
				}
			}
			if(!isOpScore(user, videoid, db))
			{
				op = false;
			}
		}
		catch(Exception e)
		{
			LogServiceImpl.log(LogLevelEnum.ERROR, e.toString());
			score = 0;
			mess = "failed";
			op = false;
			unlike = 0;
		}
		finally
		{
		    if(db != null)
		    {
			    db.close();
		    }
		}
		scoreInfo.setScore_like(score);
		scoreInfo.setMessage(mess);
		scoreInfo.setOp_able(op);
		scoreInfo.setScore_unlike(unlike);
		
		return scoreInfo;
	}

	@Override
	public ScoreInfo update_like(String user, String videoid) {
		DBConnection db = new DBConnection();
		ScoreInfo scoreInfo = new ScoreInfo();
		String mess = "failed";
		boolean opAble = true;
		Object[] params = null;
		List<Object[]> paramList = new ArrayList<Object[]>();
		int score = 0;
		int unlike = 0;
		
		try
		{
           	
			if(!isOpScore(user, videoid, db))
			{
				List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, new Object[]{videoid});
				
				if(list != null && list.size() != 0) 
				{
					Map<String, Object> map = list.get(0);
					score = Integer.parseInt(map.get("music_score").toString());
					unlike = Integer.parseInt(map.get("music_unlike").toString());
				}
				scoreInfo.setOp_able(false);
				scoreInfo.setMessage(mess);
				scoreInfo.setScore_like(score);
				scoreInfo.setScore_unlike(unlike);
				return scoreInfo;
			}
			
			String[] strSQL = null;
			if(judgeMusicInfo(videoid, db))
			{
				strSQL = new String[]{UPDATE_SQL,INSERT_MUSIC_PSN_SQL};
				params = new Object[]{ videoid };
				paramList.add(params);
				paramList.add(new Object[]{videoid, user});
			}
			else
			{
				strSQL = new String[]{INSERT_SQL, INSERT_MUSIC_PSN_SQL};
				paramList.add(new Object[]{videoid, 1, 0});
				paramList.add(new Object[]{videoid, user});
			}
			
			boolean flag = db.executeBatchInfo(strSQL, paramList);
			if(flag)
			{
			    mess = "success";	
			}
			
			List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, new Object[]{videoid});
				
			if(list != null && list.size() != 0) 
			{
				Map<String, Object> map = list.get(0);
				score = Integer.parseInt(map.get("music_score").toString());
				unlike = Integer.parseInt(map.get("music_unlike").toString());
			}
		}
		catch(Exception e)
		{
			LogServiceImpl.log(LogLevelEnum.ERROR, e.toString());
			mess = "failed";
			opAble = false;
		}
		finally
		{
			if(db != null)
			{
		        db.close();
			}
		}
		scoreInfo.setMessage(mess);
		scoreInfo.setOp_able(opAble);
		scoreInfo.setScore_like(score);
		scoreInfo.setScore_unlike(unlike);
		
		return scoreInfo;
	}
	
	
	@Override
	public ScoreInfo updaten_unlike(String user, String videoid) {
		DBConnection db = new DBConnection();
		ScoreInfo scoreInfo = new ScoreInfo();
		String mess = "failed";
		boolean opAble = true;
		Object[] params = null;
		List<Object[]> paramList = new ArrayList<Object[]>();
		int unlike = 0;
		int score = 0;
		
		try
		{
			if(!isOpScore(user, videoid, db))
			{
				List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, new Object[]{videoid});
				
				if(list != null && list.size() != 0) 
				{
					Map<String, Object> map = list.get(0);
					score = Integer.parseInt(map.get("music_score").toString());
					unlike = Integer.parseInt(map.get("music_unlike").toString());
				}
				scoreInfo.setOp_able(false);
				scoreInfo.setMessage(mess);
				scoreInfo.setScore_like(score);
				scoreInfo.setScore_unlike(unlike);
				return scoreInfo;
			}
			
			String[] strSQL = null;
			if(judgeMusicInfo(videoid, db))
			{
				strSQL = new String[]{UPDATE_TREAD_SQL, INSERT_MUSIC_PSN_SQL};
				params = new Object[]{ videoid };
				paramList.add(params);
				paramList.add(new Object[]{videoid, user});
			}
			else
			{
				strSQL = new String[]{INSERT_SQL,INSERT_MUSIC_PSN_SQL};
				paramList.add(new Object[]{videoid, 0, 1});
				paramList.add(new Object[]{videoid, user});
			}
			
			boolean flag = db.executeBatchInfo(strSQL, paramList);
			if(flag)
			{
			    mess = "success";	
			}
			
			List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, new Object[]{videoid});
			
			if(list != null && list.size() != 0) 
			{
				Map<String, Object> map = list.get(0);
				unlike = Integer.parseInt(map.get("music_unlike").toString());
				score = Integer.parseInt(map.get("music_score").toString());
			}
		}
		catch(Exception e)
		{
			LogServiceImpl.log(LogLevelEnum.ERROR, e.toString());
			mess = "failed";
			opAble = false;
		}
		finally
		{
			if(db != null)
			{
		        db.close();
			}
		}
		scoreInfo.setMessage(mess);
		scoreInfo.setOp_able(opAble);
		scoreInfo.setScore_unlike(unlike);
		scoreInfo.setScore_like(score);
		
		return scoreInfo;
	}
	
	public boolean judgeMusicInfo(String id, DBConnection db) throws Exception
	{
		Object[] params = new Object[]{id};
		List<Map<String, Object>> list = db.querySMSInfo(SELECT_BY_ID_SQL, params);
		
		if(list != null && list.size() != 0) 
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean isOpScore(String user, String videoid, DBConnection db) throws Exception
	{
		Object[] params = new Object[]{user, videoid};
		List<Map<String, Object>> list = db.querySMSInfo(GET_MUSIC_BY_PSN_SQL, params);
		
		if(list != null && list.size() != 0) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
    public final static String UPDATE_SQL = "UPDATE score SET music_score = music_score + 1 WHERE music_id = ?";
	
	public final static String INSERT_SQL = "INSERT INTO score(music_id, music_score, music_unlike) VALUES(?, ?, ?)";
	
	public final static String SELECT_BY_ID_SQL = "SELECT music_id, music_score, music_unlike FROM score WHERE music_id = ?";
	
	public final static String GET_MUSIC_BY_PSN_SQL = "SELECT music_id, psn_account FROM music_r_person WHERE psn_account = ? AND music_id = ?";
	
	public final static String INSERT_MUSIC_PSN_SQL = "INSERT INTO music_r_person(music_id, psn_account) VALUES(?, ?)";
	
	public final static String UPDATE_TREAD_SQL = "update score set music_unlike = music_unlike + 1 where music_id = ?";
	

}
